import { defineConfig } from 'rollup';
import { fileURLToPath } from 'node:url';
import { resolve, dirname } from 'node:path';
import commonjs from '@rollup/plugin-commonjs';
import typescript from '@rollup/plugin-typescript';
import resolveModule from '@rollup/plugin-node-resolve';
import json from '@rollup/plugin-json';

import packageConfig from './package.config.js';

const __dirname = dirname(fileURLToPath(import.meta.url));

export default async () => {
    const inputDir = resolve(__dirname, '../src'), // 输入目录
        outputDir = resolve(__dirname, '../bundle'), // 输出目录
        basePlugin = [resolveModule(), commonjs(), typescript(), json()], // 默认启用的 plugin
        extarnalDirname = 'external', // 外部依赖存放目录
        excludeFiles = ['type.ts']; // 打包排除文件

    const mergeConfig = await packageConfig({
        inputDir,
        outputDir,
        excludeFiles,
        extarnalDirname
    });

    mergeConfig.forEach((config) => {
        if (config.plugins?.length) config.plugins.unshift(...basePlugin);
        else config.plugins = basePlugin;
    });

    return defineConfig(mergeConfig);
};
